<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="人员名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入人员名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="resourceList">
      <el-table-column label="人员编号" prop="userId" width="120"  align="center" fixed="left" />
      <el-table-column label="人员姓名" prop="nickName" min-width="120"  align="center" />
      <el-table-column label="联系电话" prop="phonenumber" min-width="150"  align="center" >
        <template #default="scope">
          <span v-show="!scope.row.editFlag" >{{scope.row.phonenumber}}</span>
          <el-input v-show="scope.row.editFlag" v-model="scope.row.phonenumber" placeholder="请输入联系电话" style="width: 140px;"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="所在小区" prop="location" min-width="120"  align="center" />
      <el-table-column label="创建人" prop="createBy" width="120" align="center"/>
      <el-table-column label="修改人" prop="updateBy" width="120" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template v-slot="{row}">
          <el-button size="small"
                     type="text"
                     icon="CircleCheck"
                     v-show="row.editFlag"
                     @click="submit(row)">保存
          </el-button>
          <el-button size="small"
                     type="text"
                     icon="CircleClose"
                     v-show="row.editFlag"
                     @click="cancelRow(row)">取消
          </el-button>
          <el-button v-show="!row.editFlag" size="mini" type="text" icon="el-icon-edit" @click="edit(row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食材配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="人员姓名" prop="name">
          <el-input  v-model="form.name" placeholder="请输入人员姓名" />
        </el-form-item>

        <el-form-item label="联系电话" prop="phone">
          <el-input  v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="所在小区" prop="location">
          <el-select v-model="form.location"  clearable placeholder="请选择所在小区">
            <el-option label="二区" value="二区"></el-option>
            <el-option label="一区" value="一区"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form-item label="备注" prop="extra">
        <el-input  v-model="form.extra" placeholder="请输入备注" />
      </el-form-item>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, get, del, add, update } from "@/api/device/worker";
import {overflowReportUpdate} from "@/api/store/calculate/calculate";
import {getUser, updateUser} from "@/api/system/user";

export default {
  name: "Worker",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      resourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: ""
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        response.rows.map(item=>{
          item.editFlag = false;
        })
        this.resourceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {};
      this.open = true;
      this.title = "添加维保人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维保人员";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除编号为"' + row.id + '"的数据项？').then(()=> {
        let updateData = {
          userId: row.userId,
          phonenumber: row.phonenumber,
          userName:row.userName
        }
        getUser(row.userId).then(res=>{
          res.postIds.splice(res.postIds.indexOf(15),1)
          // res.postIds.indexOf(15)
          // res.postIds.find((item,index)=>{item.})
          // return
          updateData.postIds=res.postIds
          updateUser(updateData).then(res => {
            this.$modal.msgSuccess("删除成功");
            row.editFlag = false;
            this.getList();
          })
        })
      })
    },
    edit(row,index){
      row.editFlag = true;
      // this.$set(this.reportList, index, row);
    },
    submit(row){
      let updateData = {
        userId: row.userId,
        phonenumber: row.phonenumber,
        userName:row.userName
      }
      getUser(row.userId).then(res=>{
        updateData.postIds=res.postIds
        updateUser(updateData).then(res => {
          this.$modal.msgSuccess("更新成功");
          row.editFlag = false;
          this.getList();
        })
      })
    },
    cancelRow(row){
      row.editFlag = false;
      this.getList();
    },
  }
};
</script>
